package cn.bytepath.org.service.impl;

import cn.bytepath.base.service.impl.BaseServiceImpl;
import cn.bytepath.org.domain.Shop;
import cn.bytepath.org.mapper.EmployeeMapper;
import cn.bytepath.org.mapper.ShopMapper;
import cn.bytepath.org.service.IShopService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService{

    /**
     * 注入一个java邮件发送对象
     */
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 注入员工mapper对象
     */
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 注入商户mapper对象
     */
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 商户入驻时调用的方法,保存商户信息(分别保存至shop表和employee表)
     * @param shop 页面传过来的商户信息
     * @update 2022/2/10
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 1.校验shop中的信息是否重复,即判断其邮箱或者手机号是否被注册过
        // TODO: 2022/2/10
        // 2.先保存employee获取主键id
        employeeMapper.save(shop.getAdmin());
        // 3.再保存商户的shop信息(其中admin已存在主键id)
        shop.setState(0);
        shopMapper.save(shop);
    }

    /**
     * 根据id禁用商户
     * @param id 商户id
     * @update 2022/2/11
     */
    @Override
    public void forbid(Long id) {
        shopMapper.forbid(id);
    }

    /**
     * 根据id启用商户
     * @param id 商户id
     * @update 2022/2/11
     */
    @Override
    public void permit(Shop shop) throws Exception{
        shopMapper.permit(shop);

        // 创建一个复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1130074179@qq.com");
        helper.setSubject("商户入驻审核结果");

        helper.setText("<div style='color:red'><h1>恭喜您,您的宠物之家商户入驻审核结果通过</h1></div>"+
                "<img src='https://img.alicdn.com/imgextra/i1/2858327326/O1CN01jHq8xV23zMAOi4njJ_!!2858327326.jpg' />",true);
        // 添加附件
        helper.addAttachment("杰哥.rar",new File("D:\\Desktop\\images\\杰哥不要辣.rar"));
        // 收件人
        helper.setTo("myyouximail@163.com");

        javaMailSender.send(mimeMessage);
    }

    /**
     * 根据id驳回商户入驻请求
     * @param id 商户id
     * @update 2022/2/12
     */
    @Override
    public void auditBack(Shop shop) throws Exception {
        shopMapper.auditBack(shop);

        // 创建一个复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1130074179@qq.com");
        helper.setSubject("商户入驻审核结果");

        helper.setText("<div style='color:red'><h1>对不起,您的宠物之家商户入驻审核被驳回,原因如下</h1></div>" +
                        "<div style='color:orange'><h3>" + shop.getDesc() + "</h3></div>" +
                "<img src='https://img.alicdn.com/imgextra/i1/2858327326/O1CN01jHq8xV23zMAOi4njJ_!!2858327326.jpg' />",true);
        // 添加附件
        helper.addAttachment("杰哥.rar",new File("D:\\Desktop\\images\\杰哥不要辣.rar"));
        // 收件人
        helper.setTo("myyouximail@163.com");

        javaMailSender.send(mimeMessage);
    }
}
